package com.hxkj.dao.impl;

import com.hxkj.dao.UserDao;
import com.hxkj.domain.enums.UserExceptionEnum;
import com.hxkj.domain.po.SysUser;
import com.hxkj.mapper.UserMapper;
import com.hxkj.utils.AESUtil;
import com.hxkj.utils.MD5Utils;
import com.hxkj.utils.UUIDUtil;
import com.platform.common.domain.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;
import java.util.UUID;

import static java.util.Objects.nonNull;

/**
 * created by kezhijie on 2018/8/21 9:35.
 * email to kezhijie@hxoss.com
 */
@Service
public class UserDaoImpl implements UserDao {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int save(SysUser sysUser) {
        return userMapper.insert(sysUser);
    }

    @Override
    public int saveSelective(SysUser sysUser) throws Exception {
        // 添加盐标签
        sysUser.setSalt(UUID.randomUUID().toString());
        // 加盐值加密密码
        String password = MD5Utils.getEncryptedPwd(sysUser.getPassword(), sysUser.getSalt());
        sysUser.setPassword(password);
        sysUser.setUserId(UUIDUtil.generateUUID());

        if (StringUtils.hasText(sysUser.getPhone()))
            sysUser.setPhone(AESUtil.encryptPhoneNumber(sysUser.getPhone()));

        // 设置状态 0:正常，1：禁用
        if (StringUtils.isEmpty(sysUser.getUserStatus())) {
            sysUser.setUserStatus(Byte.valueOf("0"));
        }

        // 设置删除位 0：正常，1：逻辑删除
        if (StringUtils.isEmpty(sysUser.getDelTag())) {
            sysUser.setDelTag("0");
        }

        sysUser.setUserAccountUpdated("0");

        // 更新时间截
        sysUser.setCreateTime(System.currentTimeMillis());
        sysUser.setUpdateTime(System.currentTimeMillis());

        return userMapper.insertSelective(sysUser);
    }

    @Override
    public SysUser findExists(SysUser condition) {

        if (StringUtils.hasText(condition.getUserAccount()) && StringUtils.isEmpty(condition.getEmail()))
            condition.setEmail(condition.getUserAccount());

        if (StringUtils.hasText(condition.getUserAccount()) && StringUtils.isEmpty(condition.getPhone()))
            condition.setPhone(AESUtil.encryptPhoneNumber(condition.getUserAccount()));
        else if (StringUtils.hasText(condition.getPhone()))
            condition.setPhone(AESUtil.encryptPhoneNumber(condition.getPhone()));

        SysUser exists = userMapper.findExists(condition);

        if (StringUtils.hasText(condition.getPhone()))
            condition.setPhone(AESUtil.decryptPhoneNumber(condition.getPhone()));

        if (Objects.nonNull(exists) && StringUtils.hasText(exists.getPhone()))
            exists.setPhone(AESUtil.decryptPhoneNumber(exists.getPhone()));

        return exists;
    }

    @Override
    public int updateSelectiveExcludePassword(SysUser sysUser) throws Exception {

        sysUser.setUpdateTime(System.currentTimeMillis());

        return userMapper.updateSelective(sysUser);
    }

    @Override
    public int updatePassword(SysUser sysUser) throws Exception {

        sysUser.setPassword(MD5Utils.getEncryptedPwd(sysUser.getPassword(), sysUser.getSalt()));
        sysUser.setUpdateTime(System.currentTimeMillis());

        return userMapper.updatePassword(sysUser);
    }

    @Override
    public int updatePhone(SysUser sysUser) throws Exception {

        sysUser.setPhone(AESUtil.encryptPhoneNumber(sysUser.getPhone()));
        sysUser.setUpdateTime(System.currentTimeMillis());

        return userMapper.updatePhone(sysUser);
    }

    @Override
    public SysUser findByUserId(String userId) throws Exception {
        return userMapper.selectByPrimaryKey(userId);
    }

    @Override
    public int updateUserAccount(SysUser sysUser) throws Exception {
        return userMapper.updateUserAccount(sysUser);
    }
}
